# -*- coding:utf-8 -*-
"""Created By Corvo"""

from const import *
from parts.base import TipNode
from ..system.base import *

if __name__ == '__main__':
    from server import TipModuleServer


class TipModuleClient(ModuleClientBase):
    """提示模块客户端"""
    __mVersion__ = 1
    __identifier__ = ModuleEnum.identifier

    def __init__(self):
        super(TipModuleClient, self).__init__()
        self.rpc = self.ModuleSystem.CreateRpcModule(self, ModuleEnum.identifier)
        self.node_storage = {}  # 结点存储
        self.ui_bind_node = {}  # 界面绑定结点

        self.is_build_data = False  # 是否构建数据

    def OnDestroy(self):
        self.rpc.Discard()
        del self.rpc
        super(TipModuleClient, self).OnDestroy()

    @property
    def server(self):
        # type: () -> TipModuleServer
        return self.rpc

    # -----------------------------------------------------------------------------------

    """api"""

    # 注册结点
    def RegisterNode(self, node, offset=(0, 0), scale=1.0):
        # type: (TipNode, tuple, float) -> bool
        """注册结点"""

    # 反注册结点
    def UnRegisterNode(self, node):
        # type: (TipNode) -> bool
        """反注册结点"""

    # 构建结点树
    def BuildTree(self, tree):
        # type: (dict) -> bool
        """构建结点树"""
        if self.is_build_data:
            return False
        bind_cache = {}
        for node_id, config in tree.items():
            assert isinstance(config, dict)
            ui_config = config["ui"]
            ui_key = ui_config["ui_key"]

            if ui_key:
                if ui_key not in self.ui_bind_node:
                    self.ui_bind_node[ui_key] = []
                bind_node = self.ui_bind_node[ui_key]  # type: list
                bind_node.append(node_id)

            node = TipNode(node_id, ui_config)
            self.node_storage[node_id] = node
            parent_id = config.get("parent")
            if not parent_id:
                continue
            if parent_id not in self.node_storage:
                bind_cache[node_id] = parent_id
            else:
                parent = self.node_storage[parent_id]  # type: TipNode
                parent.AddChild(node)
                node.SetParent(parent)
        # 构建剩余结点
        for node_id, parent_id in bind_cache.items():
            if parent_id not in self.node_storage:
                raise Exception("Fail to build parent node: %s" % parent_id)
            child = self.node_storage[node_id]  # type: TipNode
            parent = self.node_storage[parent_id]  # type: TipNode
            parent.AddChild(child)
            child.SetParent(parent)
        print "[suc]", "finished build node tree: %s" % len(self.node_storage)
        # 构建绑定关系
        self.is_build_data = tree
        return True

    # 更新指定界面全部结点
    def UpdateTargetUiNode(self, ui_key):
        # type: (str) -> bool
        """
        更新指定界面全部结点\n
        - 一般用于界面的Create后执行
        """
        if ui_key not in self.ui_bind_node:
            return False
        ui_node = self.GetUINode(ui_key)
        if not ui_node:
            return False
        for node_id in self.ui_bind_node[ui_key]:
            node = self.node_storage[node_id]  # type: TipNode
            node.UpdateBindUi()
        return True

    # 设置结点状态
    def SetNodeState(self, node_id, state):
        # type: (str, int) -> bool
        """更新结点数据"""
        if node_id not in self.node_storage:
            return False
        node = self.node_storage[node_id]  # type: TipNode
        return node.SetState(state)

    # 获得结点状态
    def GetNodeState(self, node_id):
        # type: (str) -> int
        """获得结点状态"""
        node = self.node_storage.get(node_id)  # type: TipNode
        if not node:
            return -1
        return node.GetState()

    # 获得结点子结点总显示
    def GetNodeChildOnNum(self, node_id):
        # type: (str) -> int
        """获得结点子结点总显示"""
        node = self.node_storage.get(node_id)  # type: TipNode
        if not node:
            return 0
        return node.GetChildOnNum()

    # 是否为端点结点
    def IsEndNode(self, node_id):
        # type: (str) -> bool
        """是否为端点结点"""
        node = self.node_storage.get(node_id)  # type: TipNode
        if not node:
            return False
        return node.IsEndNode()
